<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :lg="6" :xs="24">
        <div class="head-container" style="margin-bottom:10px;padding-left: 60px;">
          <span
            style="font-family: 'Arial Normal', 'Arial',serif;font-weight: bold;font-size: 20px;letter-spacing: normal;line-height: normal; font-style: normal;color: #333333;text-align: center;">组织结构</span>
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            :default-expanded-keys="[100]"
            node-key="id"
            show-checkbox
            @check="handleCheckChange"
          >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span v-if="data.label">
              <a class="treeTitle" :title="data.label">{{data.label | ellipsis}}</a>
            </span>
          </span>
        </el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="18" :lg="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-if="showSearch"
                 label-width="68px">
          <el-form-item prop="title">
            <el-input style="cursor: pointer" v-model="queryParams.title" @focus="openPickActive"
                      placeholder="请选择任务名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">统计</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- <div style="margin-bottom: 8px;margin-top: -5px">{{ deptPickList.map(m => m.label).join(",") }}</div> -->
        <el-empty v-if="!showTable" description="请选择部门和任务进行统计"></el-empty>
        <el-table v-if="showTable" v-loading="loading" :data="statData" style="margin-bottom: 30px">
          <el-table-column label="参与人数" align="center" prop="allCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="allcountHanlder">{{ scope.row.allCount }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="优秀人数" align="center" prop="excellentCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="excellentHanlder">{{
                  scope.row.excellentCount
                }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="优秀占比" align="center" prop="excellentScale">
            <template slot-scope="scope">
              {{ computeScale(scope.row.excellentCount,scope.row.allCount) + '%' }}
            </template>
          </el-table-column>
          <el-table-column label="良好人数" align="center" prop="goodCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="goodHanlder">{{ scope.row.goodCount }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="良好占比" align="center" prop="goodScale">
            <template slot-scope="scope">
              {{ computeScale(scope.row.goodCount,scope.row.allCount) + '%' }}
            </template>
          </el-table-column>
          <el-table-column label="称职人数" align="center" prop="qualifiedCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="qualifiedHanlder">{{
                  scope.row.qualifiedCount
                }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="称职占比" align="center" prop="qualifiedScale">
            <template slot-scope="scope">
              {{ computeScale(scope.row.qualifiedCount,scope.row.allCount) + '%' }}
            </template>
          </el-table-column>

          <el-table-column label="基本称职人数" align="center" prop="almostCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="almostHanlder">{{
                  scope.row.almostCount
                }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="基本称职占比" align="center" prop="almostScale">
            <template slot-scope="scope">
              {{ computeScale(scope.row.almostCount,scope.row.allCount) + '%' }}
            </template>
          </el-table-column>

          <el-table-column label="不称职人数" align="center" prop="unqualifiedCount">
            <template slot-scope="scope">
              <el-link type="primary" :underline="false" @click="unqualifiedHanlder">
                {{ scope.row.unqualifiedCount }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="不称职占比" align="center" prop="unqualifiedScale">
            <template slot-scope="scope">
              {{ computeScale(scope.row.unqualifiedCount,scope.row.allCount) + '%' }}
            </template>
          </el-table-column>
        </el-table>
        <div v-show="showPie" id="myChartsPie" style="width: 100%;height: 800px"></div>
      </el-col>
    </el-row>

    <el-dialog title="任务列表" :visible.sync="open" width="800px" >
      <el-form :model="pickParams" ref="pickParams" size="small" :inline="true" label-width="68px">
        <el-form-item prop="title">
          <el-input
            v-model="pickParams.title"
            placeholder="请输入任务标题"
            clearable
            @keyup.enter.native="handlePickQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handlePickQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetPickQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="pickLoading" ref="singleTable" :data="pickData"
                @current-change="handleCurrentChange"
                style="width: 100%">
        <el-table-column
          type="index"
          width="50">
          <template slot-scope="scope">
            <el-radio  v-model="currentData" :label="scope.row"><i></i></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          property="title"
          label="任务名称"
         >
        </el-table-column>
        <el-table-column
          property="examineUserName"
          label="创建人"
         >
        </el-table-column>
        <el-table-column
          property="createTime"
          label="创建时间">
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pickParams.pageNum"
        :limit.sync="pickParams.pageSize"
        @pagination="getActivePick"
      />
      <div style="text-align: center; margin-top: 40px">
        <el-button type="primary" @click="pickSubmit">确 定</el-button>
        <el-button @click="pickCancel">取 消</el-button>
      </div>
    </el-dialog>
    <statistics-dialog ref="statisticsDialogRef" :export-method="handleExport"></statistics-dialog>
  </div>
</template>

<script>
import {treeselect,treeselectAll} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import echarts from 'echarts'
import quarter from "@/views/tool/quarter";
import halfyear from "@/views/tool/halfyear";
import {taskStatistics} from "@/api/examine/task/api";
import {listPick} from "@/api/examine/active/api";
import statisticsDialog from "@/views/examine/statisticsDialog"

export default {
  filters:{
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 10) {
        return value.slice(0, 10) + "...";
      }
      return value;
    }
  },
  name: "statistics",
  dicts: ['sys_normal_disable', 'sys_user_sex', 'examine_cycle'],
  components: {
    Treeselect,
    quarter,
    halfyear,
    statisticsDialog
  },
  data() {
    return {
      pickLoading: false,
      chartDOMPie: null,
      open: false,
      total: 0,
      currentData: {},
      pickParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        missionStatus: 1,
        examineStatus: 2,
        examineType: 1,
        approveStatus: 1
      },
      cycleVal: '',
      showTable: false,
      showPie: false,
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      statData: [],
      pickData: [],
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      userTipOptions: [],
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      deptPickList: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      examineYearOptions: {
        disabledDate: (time) => {
          const date = new Date()
          const year = date.getFullYear()
          const timeYear = time.getFullYear()
          return year > timeYear + 3
        }
      },
      // 查询参数
      queryParams: {
        examineCycle: undefined,
        pickCycle: undefined,
        deptSearchStr: '',
        examineResult: undefined,
        missionStatus: 1,
        examineStatus: 2,
        examineType: 1,
        approveStatus: 1,
        id: undefined,
        title: undefined
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    // this.getList();
    this.getTreeselect();
  },
  mounted() {
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      taskStatistics(this.queryParams).then(response => {
        this.statData = [response.data];
        if (this.statData[0].allCount < 1) {
          this.statData = [];
        } else {
          this.showTable = true
        }
        if (this.deptPickList.length > 0 && this.queryParams.title) {
          this.showTable = true
          this.showPie = true
          this.$nextTick(() => {
            this.initPieCharts(response.data)
          })
        } else {
          this.showTable = false
          this.showPie = false
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    computeScale(item,all) {
      const number= ((item/all)*100).toFixed(2)
      return isFinite(number) && number || 0;
    },
    initPieCharts(data) {
      var chartDOMPie = echarts.init(document.getElementById("myChartsPie"));
      chartDOMPie.setOption({
        legend: {
          orient: 'horizontal',
          left: 'right'
        },
        color: ['#3bb2fb', '#f2a694', '#f8ae30', '#c093de'],
        series: [
          {
            center: ['45%', '30%'],
            type: 'pie',
            avoidLabelOverlap: true,
            radius: '50%',
            data: [{value: data.excellentCount, name: '优秀'},
              {value: data.goodCount, name: '良好'},
              {value: data.qualifiedCount, name: '称职'},
              {value: data.almostCount, name: '基本称职'},
              {value: data.unqualifiedCount, name: '不称职'}
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              normal: {
                formatter: '{c}人 ({d}%)',
                textStyle: {fontSize: "14", fontWeight: "bold"}
              }
            }
          }
        ]
      })
      chartDOMPie.on('click', (param) => {
        var index = param.dataIndex
        if (index == 0) {
          this.excellentHanlder()
        } else if (index == 1) {
          this.goodHanlder()
        } else if (index == 2) {
          this.qualifiedHanlder()
        } else if (index == 3) {
          this.unqualifiedHanlder()
        }
      })
    },
    // 全部跳转
    allcountHanlder() {
      this.queryParams.isSubmitted=null;
      this.queryParams.examineResult = null;
      this.routerJumper();
    },
    // 优秀跳转
    excellentHanlder() {
      this.queryParams.isSubmitted=1
      this.queryParams.examineResult = 3;
      this.routerJumper();
    },
    // 良好跳转
    goodHanlder() {
      this.queryParams.isSubmitted=1
      this.queryParams.examineResult = 2;
      this.routerJumper();
    },
    // 称职跳转
    qualifiedHanlder() {
      this.queryParams.isSubmitted=1
      this.queryParams.examineResult = 1;
      this.routerJumper();
    },
    // 不称职跳转
    unqualifiedHanlder() {
      this.queryParams.isSubmitted=1
      this.queryParams.examineResult = 0;
      this.routerJumper();
    },
    // 不称职跳转
    almostHanlder() {
      this.queryParams.isSubmitted=1
      this.queryParams.examineResult = 4;
      this.routerJumper();
    },
    routerJumper() {
      if(this.deptPickList){
        this.queryParams.deptSearchStr= this.deptPickList.map(m => m.id).join(",")
      }
      this.$refs.statisticsDialogRef.dialogOpen(this.queryParams);
      // this.$router.push({path: "/task/detail", query: {params: this.queryParams}});
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselectAll().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleCheckChange() {
      //获取所有选中的节点id
      this.tenantIdMenus = this.$refs.tree.getCheckedNodes()
      this.deptPickList=this.tenantIdMenus
    },
    getCycleValue(data) {
      this.cycleVal = data;
    },
    // 表单重置
    reset() {
      this.form = {
        examineCycle: undefined,
        pickCycle: undefined
      };
      this.cycleVal = '';
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.deptPickList) {
        this.queryParams.deptSearchStr = this.deptPickList.map(m => m.id).join(",")
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tree.setCheckedKeys([]);
      this.deptPickList = []
      this.handleQuery();
    },
    // 季度选择回调
    getQuarter(val) {
      this.queryParams.pickCycle = val
    },
    // 半年度选择回调
    getHalfyear(val) {
      this.queryParams.pickCycle = val
    },
    getActivePick() {
      this.pickLoading = true;
      listPick(this.pickParams).then(resp => {
        this.pickData = resp.rows
        this.total = resp.total;
      }).finally(() => {
        this.pickLoading = false;
      })
    },
    handlePickQuery() {
      this.getActivePick()
    },
    resetPickQuery() {
      this.resetForm("pickParams");
      this.getActivePick();
    },
    openPickActive() {
      this.open = true
      this.getActivePick();
      if (this.currentData) {
        this.$nextTick(() => {
          this.$refs.singleTable.setCurrentRow(this.currentData);
        })
      }
    },
    pickSubmit() {
      this.queryParams.title = this.currentData.title
      this.queryParams.id = this.currentData.id
      this.open = false;
    },
    handleCurrentChange(selection) {
      this.currentData = selection;
    },
    pickCancel() {
      this.resetForm("pickParams");
      this.open = false
    },
    /** 导出按钮操作 */
    handleExport() {
      let examineResult = this.queryParams.examineResult;
      var text = '';
      if (examineResult == null) {
        text = '全部';
      } else if (examineResult == 3) {
        text = '优秀';
      } else if (examineResult == 2) {
        text = '良好';
      } else if (examineResult == 1) {
        text = '称职';
      } else if (examineResult == 0){
        text = '不称职';
      } else if (examineResult == 4){
        text = '基本称职';
      }
      this.download('examine/task/detail/export', {
        ...this.queryParams
      }, "任务考评" + text + "人员数据导出.xlsx")
    },
  }
};
</script>
<style lang="scss" scoped>
</style>
